﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Service
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "WCF服务器";
            using (ServiceHost host = new ServiceHost(typeof(MyService), new Uri("http://127.0.0.1:1211/sv")))
            {
                //绑定
                //BasicHttpBinding binding = new BasicHttpBinding();
                //binding.Security.Mode = BasicHttpSecurityMode.None;
                //host.AddServiceEndpoint(typeof(IService), binding, "/ep");

                NetTcpBinding binding = new NetTcpBinding();
                binding.Security.Mode = SecurityMode.None;
                host.AddServiceEndpoint(typeof(IService), binding, "net.tcp://127.0.0.1:2377/ep");

                ServiceMetadataBehavior mb = new ServiceMetadataBehavior();
                mb.HttpGetEnabled = true;
                mb.HttpGetUrl = new Uri("http://127.0.0.1:8008/meta");
                host.Description.Behaviors.Add(mb);
                host.Opened += (sender, arg) =>
                    {
                        Console.WriteLine("服务已启动。");
                    };
                try
                {
                    host.Open();
                    Console.Read();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }

    [ServiceContract(SessionMode = SessionMode.Required)]
    public interface IService
    {
        [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)]
        void SetValue(int n);
        [OperationContract]
        int GetValue();
        [OperationContract(IsInitiating = false, IsTerminating = true)]
        void EndSession();
    }

    public class MyService : IService
    {
        public MyService()
        {
            Console.WriteLine("-------------------------------------");
            Console.WriteLine("当前实例的hashcode是：" + this.GetHashCode());
            Console.WriteLine("{0}----服务被实例化。", DateTime.Now.ToLongTimeString());
        }
        ~MyService()
        {
            Console.WriteLine("当前实例的hashcode是：" + this.GetHashCode());
            Console.WriteLine("{0}----服务被释放。", DateTime.Now.ToLongTimeString());
            Console.WriteLine("-------------------------------------");
        }
        /// <summary>   
        /// 私有字段   
        /// </summary>   
        private int m_Value = int.MinValue;

        public void SetValue(int n)
        {
            this.m_Value = n;
            Console.WriteLine("会话ID：{0}", OperationContext.Current.SessionId);
        }

        public int GetValue()
        {
            Console.WriteLine("会话ID：{0}", OperationContext.Current.SessionId);
            return this.m_Value;
        }
        public void EndSession()
        {
            Console.WriteLine("会话结束！");
        }

    }
}
